package com.zerdoor.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.IArtistPersonalHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/14.
 * 艺术家个人主页 控制层（包括主页、作品专辑、作品集）
 */
@Controller
@RequestMapping("/artistPersonalHome")
public class ArtistPersonalHomeController {

    @Autowired
    private IArtistPersonalHomeService artistPersonalHomeService;





    /**
     * 查询艺术家个人主页
     * @param receiver 艺术家ID
     * @return
     */
    @RequestMapping(value = "/artistPersonalHome", method = RequestMethod.GET)
    public String findArtistPersonalHomeByUserId(int receiver,HttpServletRequest request) {


        try {


            Map<String,Object> map =  artistPersonalHomeService.findArtistPersonalHome(receiver);


            request.setAttribute("artistHomeMap",map);

//            ResponseEntity<Map<String,Object>> responseEntity = new ResponseEntity<Map<String,Object>>(map, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();

//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }

        return "/receiver/artistPersonalHome";

    }

    /**
     * 根据艺术家ID查询已完成作品 分页查询
     * @param param 艺术家ID
     * @return
     */
    @RequestMapping(value = "/findFinishProductionByReceiver", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findFinishProductionByReceiver(@RequestParam Map param) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");
            int receiver = Integer.parseInt(param.get("receiver") + "");
            PageHelper.startPage(page, rows);

            List<Map<String,String>> list =  artistPersonalHomeService.findFinishProductionByReceiver(receiver);

            pageInfo =  new PageInfo(list);


        } catch (Exception e) {

            e.printStackTrace();

//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }

        return pageInfo;

    }


    /**
     * 根据艺术家ID查询出售中作品 分页查询
     * @param param 艺术家ID
     * @return
     */
    @RequestMapping(value = "/findSalesProductionByReceiver", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findSalesProductionByReceiver(@RequestParam Map param) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");
            int receiver = Integer.parseInt(param.get("receiver") + "");
            PageHelper.startPage(page, rows);

            List<Map<String,String>> list =  artistPersonalHomeService.findSalesProductionByReceiver(receiver);

            pageInfo =  new PageInfo(list);


        } catch (Exception e) {

            e.printStackTrace();

        }

        return pageInfo;

    }


    /**
     * 根据艺术家ID、开始日期、结束日期查询评价详细数据 分页查询
     * @param param 艺术家ID
     * @return
     */
    @RequestMapping(value = "/findAppraiseInfoByReceiver", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findAppraiseInfoByReceiver(@RequestParam Map param) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");
            PageHelper.startPage(page, rows);


            pageInfo =  new PageInfo(artistPersonalHomeService.findAppraiseInfoByReceiver(param));


        } catch (Exception e) {

            e.printStackTrace();

        }

        return pageInfo;

    }


    /**
     * 查询艺术家作品专辑
     * @param param 艺术家ID
     * @return
     */
    @RequestMapping(value = "/findUserPhotoByUserId", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findUserPhotoByUserId(@RequestParam Map param) {

        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");

            PageHelper.startPage(page, rows);

            //取出艺术家ID
            int artist_id = Integer.parseInt(param.get("artist_id") + "");

            List<Map<String,Object>> list =  artistPersonalHomeService.findUserPhotoByUserId(artist_id);

            pageInfo =  new PageInfo(list);
//            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();

//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }

        return pageInfo;


    }


    /**
     * 跳转到艺术家个人主页-作品专辑
     * @return
     */
    @RequestMapping(value = "/artistPersonalSpecial", method = RequestMethod.GET)
    public String artistPersonalSpecial(int artistId,HttpServletRequest request) {


        try {
            Map navigation = artistPersonalHomeService.findNavigationByArtistId(artistId);
            //导航
            request.setAttribute("navigation",navigation);

        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("artistId",artistId);


        return "/receiver/artistPersonalSpecial";
    }
    /**
     * 跳转到作品集列表-首页
     * @return
     */
    @RequestMapping(value = "/artistProduction", method = RequestMethod.GET)
    public String artistProduction(int photo_id,HttpServletRequest request) {

        request.setAttribute("photo_id",photo_id);


        try {
            Map navigation = artistPersonalHomeService.findNavigationByPhotoId(photo_id);
            request.setAttribute("navigation",navigation);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/receiver/artistProduction";
    }


    /**
     * 分页查询作品集列表
     * @param param 参数
     * @return
     */
    @RequestMapping(value = "/findUserProductionByPhotoId", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findProductionList(@RequestParam Map param) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");

            PageHelper.startPage(page, rows);

            List<Map<String,String>> list =  artistPersonalHomeService.selectAllUserProductionByPhotoId(param);

            pageInfo =  new PageInfo(list);
//            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();


//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }


        return pageInfo;

    }

    /**
     * 跳转到术家个人主页-作品集浏览
     * @return
     */
    @RequestMapping(value = "/browseProduction", method = RequestMethod.GET)
    public String browseProduction(int production_id,HttpServletRequest request) {

        Map<String,Object> data = null;
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        int userID = 0;
        if(userInfo != null){
            userID = userInfo.getUserId();
        }

        try {
            data = artistPersonalHomeService.updatefindUserProductionByProductionId(production_id,userID);

        } catch (Exception e) {
            e.printStackTrace();
        }


        request.setAttribute("productionInfo",data);


        return "/receiver/browseProduction";
    }


    /**
     * 确认作品订单页面
     * @return
     */
    @RequestMapping(value = "/confirmPersonalOrder", method = RequestMethod.GET)
    public String confirmPersonalOrder(int production_id,HttpServletRequest request) {

        Map<String,Object> data = null;
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        int userID = 0;
        if(userInfo != null){
            userID = userInfo.getUserId();
        }

        try {
            data = artistPersonalHomeService.updatefindUserProductionByProductionId(production_id,userID);

        } catch (Exception e) {
            e.printStackTrace();
        }


        request.setAttribute("productionInfo",data);


        return "/receiver/confirmPersonalOrder";
    }
    /**
     * 去支付页面
     * @return
     */
    @RequestMapping(value = "/publishAndPay", method = RequestMethod.GET)
    public String publishAndPay(int production_id,HttpServletRequest request) {

        Map<String,Object> data = null;
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

        int userID = 0;
        if(userInfo != null){
            userID = userInfo.getUserId();
        }

        try {
            data = artistPersonalHomeService.updatefindUserProductionByProductionId(production_id,userID);

        } catch (Exception e) {
            e.printStackTrace();
        }


        request.setAttribute("productionInfo",data);


        return "/receiver/publishAndPay";
    }


    /**********************购买***********************/
    /**
     * paypay支付增值服务费用
     * @param works_id 作品ID
     * @param paymentType 支付类型 1为paypay支付，2为支付宝支付，3为余额支付
     * @return
     */
    @RequestMapping(value = "/buyWorkByPaypay", method = RequestMethod.POST)
    public ResponseEntity<?> buyWorkByPaypay(String works_id,String paymentType,HttpServletRequest request) {



        try {


            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            int userID = userInfo.getUserId();
            boolean flag = false;
            if("1".equals(paymentType)){
                flag = artistPersonalHomeService.updateBuyWorkByPaypay(works_id,userID);
            }else if("2".equals(paymentType)){

                flag = artistPersonalHomeService.updateBuyWorkByAlipay(works_id,userID);

            }else if("3".equals(paymentType)){

                flag = artistPersonalHomeService.updateBuyWorkByBalance(works_id,userID);

            }else{
                new Exception("无效的支付类型！");
            }




            ResponseEntity<Boolean> responseEntity = new ResponseEntity<Boolean>(flag, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }




    }


}
